<template>
  <div class="v-leave">

    <div v-if="apart">
      <div class="reason-box">
        <textarea class="reason" v-model="reason" placeholder="请输入离职原因及想要离职的日期" maxlength="280"
                  @blur="inputBlur"></textarea>
      </div>
      <div class="tip">温馨提示：驻厂接到离职通知后，会尽快帮您办理离职手续</div>
      <div class="btn">
        <button type="button" class="confirm" @click="leave">确定</button>
      </div>
      <div class="btn">
        <router-link to="/entry">
          <button type="button" class="cancel">取消</button>
        </router-link>
      </div>
    </div>
    <div v-if="comeback">
      <div class="reason-back" v-model="reasonBack">{{reasonBack}}</div>
      <div class="abolish-btn">
        <button type="button" class="abolish" @click="abolish">取消离职通知</button>
      </div>
    </div>
  </div>
</template>

<script>
  import {baseUrl} from "../../common/js/env"

  export default {
    data() {
      return {
        reason: "",
        apart: false,
        comeback: false,
        reasonBack: "",
        userSign: ""
      }
    },
    created() {
      //是否有离职通知
      this.$vux.loading.show({
        text: '加载中',
      })
      let base64 = require('js-base64').Base64;
      this.userSign = base64.decode(sessionStorage.getItem("user_sign"));
      this.axios.get(baseUrl + "/dimission/get_notice?sgin=" + this.userSign).then((res) => {
        let resultObj = res.data;
        this.$vux.loading.hide();
        if (resultObj.code == 0 && resultObj.data != null) {
          if (resultObj.data.isNotice == 1) { //1为有离职通知
            this.comeback = true;
            this.reasonBack = resultObj.data.details;
          } else {
            this.apart = true;
          }
        } else {
          this.$vux.toast.text(resultObj.msg, "middle");
          return;
        }
      }).catch(() => {
        this.$vux.loading.hide();
        this.$vux.toast.text("网络异常", "middle");
      });
    },
    methods: {
      leave() {//离职
        if (this.reason == null || this.reason == "") {
          this.$vux.toast.text("请输入离职原因和离职日期", "middle");
          return;
        }
        this.$vux.loading.show({
          text: '加载中',
        })
        this.axios.get(baseUrl + "/dimission/add_dimission?sgin=" + this.userSign + "&reason=" + this.reason + "&type=" + 1).then((res) => {
          let resultObj = res.data;
          this.$vux.loading.hide();
          if (resultObj.code == 0 && resultObj.data != null) {
            if (resultObj.data == 1) {
              this.$router.push('/entry');
            } else {
              this.$vux.toast.text("提交失败,请重新提交", "middle");
              return;
            }
          } else {
            this.$vux.toast.text(resultObj.msg);
            return;
          }
        }).catch(() => {
          this.$vux.loading.hide();
          this.$vux.toast.text("网络异常", "middle");
        });
      },
      abolish() {//取消离职
        this.$vux.loading.show({
          text: '加载中',
        })
        this.axios.get(baseUrl + "/dimission/add_dimission?sgin=" + this.userSign + "&reason=" + this.reasonBack + "&type=" + 2).then((res) => {
          let resultObj = res.data;
          this.$vux.loading.hide();
          if (resultObj.code == 0) {
            if (resultObj.data == 1) {
              this.$router.push('/entry');
            } else {
              this.$vux.toast.text("取消失败,请重新取消", "middle");
              return;
            }
          } else {
            this.$vux.toast.text(resultObj.msg, "middle");
            return;
          }
        }).catch(() => {
          this.$vux.loading.hide();
          this.$vux.toast.text("网络异常", "middle");
        });
      },
      inputBlur() {
        window.scrollTo(0, 0)
      }
    }
  }
</script>

<style lang="stylus" scoped>
  .v-leave
    width: 100%
    height: 100%
    overflow: hidden
    .reason-box
      display: flex
      justify-content: center
      width: 92.53%
      height: 3.507rem
      margin: 0 auto
      margin-top: 0.387rem
      .reason
        width: 100%
        height: 100%
        padding-left: 0.213rem
        padding-top: 0.187rem
        border: 1px solid #939393
        color: rgba(51, 51, 51, 1)
        font-size: 0.387rem
        resize: none
    .tip
      margin-top: 0.6rem
      margin-bottom: 5.053rem
      text-align: center
      font-size: 0.347rem
      color: rgba(102, 102, 102, 1)
    .btn
      width: 100%
      height: 1.333rem
      text-align: center
      .confirm
        width: 92.53%
        height: 1.333rem
        line-height: 1.333rem
        background: rgba(60, 175, 255, 1)
        border-radius: 0.107rem
        font-size: 0.48rem;
        color: rgba(255, 255, 255, 1)
      .cancel
        margin-top: 0.867rem
        width: 92.53%
        height: 1.333rem
        line-height: 1.333rem
        border: 1px solid rgba(60, 175, 255, 1)
        border-radius: 0.107rem
        background: #FFF
        font-size: 0.48rem;
        color: rgba(60, 175, 255, 1)
    .abolish-btn
      position: fixed
      bottom: 0.48rem
      width: 100%
      height: 1.333rem
      text-align: center
      .abolish
        width: 92.53%
        height: 1.333rem
        line-height: 1.333rem
        background: rgba(60, 175, 255, 1)
        border-radius: 0.107rem
        font-size: 0.48rem
        color: rgba(255, 255, 255, 1)
    .reason-back
      width: 89.6%
      height: auto
      margin 0 auto
      margin-top: 0.6rem
      font-size: 0.387rem
      color: rgba(68, 68, 68, 1)
      line-height: 0.56rem
</style>
